package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NotesGUI extends JFrame implements ActionListener {
    private JPanel panelNotes;
    private JPanel panelSaleInvoice;
    private JPanel panelSaleInvoice2;
    private JPanel panelLeaseInvoice;
    private JPanel panelLeaseInvoice2;
    private JPanel panelDispatchNote;
    private JPanel panelDispatchNote2;
    private JPanel panelDeliveryNote;
    private JPanel panelDeliveryNote2;
    private Container container;
    private NoteController noteController;

    public NotesGUI() {
        panelNotes = new JPanel(new GridBagLayout());
        panelSaleInvoice = new JPanel(new GridBagLayout());
        panelSaleInvoice2 = new JPanel(new GridBagLayout());
        panelDeliveryNote = new JPanel(new GridBagLayout());
        panelDeliveryNote2 = new JPanel(new GridBagLayout());
        panelLeaseInvoice = new JPanel(new GridBagLayout());
        panelLeaseInvoice2 = new JPanel(new GridBagLayout());
        panelDispatchNote = new JPanel(new GridBagLayout());
        panelDispatchNote2 = new JPanel(new GridBagLayout());
        noteController = new NoteController();

        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(600,470);
        this.setResizable(false);

        notesMainMenu();
    }

    public void actionPerformed(ActionEvent e){
        //if(e.getSource() == exitItem){
        //System.exit(0);
        //}
    }

    public void makeNotesMenu() {
        GridBagConstraints gbc = new GridBagConstraints();

        JButton invoiceForSale = new JButton("Invoice for sale");
        invoiceForSale.setPreferredSize(new Dimension(150,25));
        invoiceForSale.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findSaleInvoice();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelNotes.add(invoiceForSale, gbc);

        JButton invoiceForLease = new JButton("Invoice for lease");
        invoiceForLease.setPreferredSize(new Dimension(150,25));
        invoiceForLease.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findLeaseInvoice();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelNotes.add(invoiceForLease, gbc);

        JButton dispatchNote = new JButton("Dispatch note");
        dispatchNote.setPreferredSize(new Dimension(150,25));
        dispatchNote.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findDispatchNote();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelNotes.add(dispatchNote, gbc);

        JButton deliveryNote = new JButton("Delivery note");
        deliveryNote.setPreferredSize(new Dimension(150,25));
        deliveryNote.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findDeliveryNote();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelNotes.add(deliveryNote, gbc);

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    dispose();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelNotes.add(back, gbc);
    }

    public void notesMainMenu(){
        makeNotesMenu();
        this.getContentPane().add(panelNotes, BorderLayout.NORTH);
    }

    private void generateSaleInvoice(){
        JTextField textSaleId = new JTextField();
        JLabel labelSaleId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textSaleId.setText("");

        labelSaleId = new JLabel("Sale ID:", JLabel.RIGHT);
        labelSaleId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice.add(labelSaleId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textSaleId.setPreferredSize(new Dimension(100,25));
        panelSaleInvoice.add(textSaleId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findSaleInvoiceSecondWindow(Integer.parseInt(textSaleId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textSaleId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findSaleInvoice();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No note!");
                        textSaleId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelSaleInvoice.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    notesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textSaleId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelSaleInvoice.add(back,gbc);
    }

    public void findSaleInvoice(){
        generateSaleInvoice();
        this.getContentPane().add(panelSaleInvoice, BorderLayout.NORTH);
    }

    public void generateSaleInvoiceSecondWindow(int saleId){
        Sale sale = SaleContainer.getInstance().findSaleById(saleId);
        Date dNow = new Date( );
        SimpleDateFormat ft =  new SimpleDateFormat ("dd/MM/yyyy");

        JTextField textEmployeeId = new JTextField();
        JTextField textEmployeeName = new JTextField();
        JTextField textSaleDate = new JTextField();
        JTextField textCustomerId = new JTextField();
        JTextField textCustomerName = new JTextField();
        JTextField textCustomerAddress = new JTextField();
        JLabel labelEmployeeId;
        JLabel labelEmployeeName;
        JLabel labelSaleDate;
        JLabel labelCustomerId;
        JLabel labelCustomerName;
        JLabel labelCustomerAddress;

        JLabel labelProducts;
        JLabel labelProduct;
        JLabel labelAmount;
        JLabel labelUnitPrice;
        JLabel labelPrice;
        JTextField textProduct = new JTextField();
        JTextField textAmount = new JTextField();
        JTextField textUnitPrice = new JTextField();
        JTextField textPrice = new JTextField();

        JLabel labelNull;
        JLabel labelFinishPrice;
        JLabel labelDiscount;
        JLabel labelTotalPrice;
        JLabel labelDate;
        JTextField textFinishPrice = new JTextField();
        JTextField textDiscount = new JTextField();
        JTextField textTotalPrice = new JTextField();
        JTextField textDate = new JTextField();
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textEmployeeId.setEditable(false);
        textEmployeeName.setEditable(false);
        textSaleDate.setEditable(false);
        textCustomerId.setEditable(false);
        textCustomerName.setEditable(false);
        textCustomerAddress.setEditable(false);
        textProduct.setEditable(false);
        textAmount.setEditable(false);
        textUnitPrice.setEditable(false);
        textPrice.setEditable(false);
        textFinishPrice.setEditable(false);
        textDiscount.setEditable(false);
        textTotalPrice.setEditable(false);
        textDate.setEditable(false);

        textEmployeeId.setText(Integer.toString(sale.getEmployeeID()));
        textEmployeeName.setText(EmployeeContainer.getInstance().findEmployeeById(sale.getEmployeeID()).getName());
        textSaleDate.setText(sale.getDate());
        textCustomerId.setText(Integer.toString(sale.getCustomerID()));
        textCustomerName.setText(CustomerContainer.getInstance().findCustomerById(sale.getCustomerID()).getName());
        textCustomerAddress.setText(CustomerContainer.getInstance().findCustomerById(sale.getCustomerID()).getAddress());
        textDiscount.setText(CustomerContainer.getInstance().findCustomerById(sale.getCustomerID()).getDiscount() + "%");
        textFinishPrice.setText(Double.toString(sale.getTotalPrice()));
        textDate.setText(ft.format(dNow));

        labelEmployeeId = new JLabel("Employee ID:", JLabel.RIGHT);
        labelEmployeeId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelEmployeeId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeId.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textEmployeeId,gbc);

        labelEmployeeName = new JLabel("Employee Name:", JLabel.RIGHT);
        labelEmployeeName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelEmployeeName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeName.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textEmployeeName,gbc);

        labelSaleDate = new JLabel("Sale date:", JLabel.RIGHT);
        labelSaleDate.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelSaleDate, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textSaleDate.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textSaleDate,gbc);

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelCustomerId, gbc);

        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textCustomerId,gbc);

        labelCustomerName = new JLabel("Customer Name:", JLabel.RIGHT);
        labelCustomerName.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelCustomerName, gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerName.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textCustomerName,gbc);

        labelCustomerAddress = new JLabel("Customer Address:", JLabel.RIGHT);
        labelCustomerAddress.setPreferredSize(new Dimension(150,25));
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelCustomerAddress,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerAddress.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textCustomerAddress,gbc);

        labelProducts = new JLabel("Products:", JLabel.CENTER);
        labelProducts.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelProducts,gbc);

        labelProduct = new JLabel("Product:", JLabel.LEFT);
        labelProduct.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelProduct,gbc);

        labelAmount = new JLabel("Amount:", JLabel.LEFT);
        labelAmount.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelAmount,gbc);

        labelUnitPrice = new JLabel("Unit Price:", JLabel.LEFT);
        labelUnitPrice.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelUnitPrice,gbc);

        labelPrice = new JLabel("Price:", JLabel.LEFT);
        labelPrice.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 3;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelPrice,gbc);

        int y = 5;
        double sum =0;
        for(int i=0; i<sale.getProducts().size(); i++){

            textProduct = new JTextField();
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textProduct.setPreferredSize(new Dimension(80,25));
            panelSaleInvoice2.add(textProduct,gbc);

            textAmount = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textAmount.setPreferredSize(new Dimension(80,25));
            panelSaleInvoice2.add(textAmount,gbc);

            textUnitPrice = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textUnitPrice.setPreferredSize(new Dimension(80,25));
            panelSaleInvoice2.add(textUnitPrice,gbc);

            textPrice = new JTextField();
            gbc.gridx = 3;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textPrice.setPreferredSize(new Dimension(80,25));
            panelSaleInvoice2.add(textPrice,gbc);

            y++;
            textProduct.setEditable(false);
            textAmount.setEditable(false);
            textUnitPrice.setEditable(false);
            textPrice.setEditable(false);

            int key = (int) sale.getProducts().keySet().toArray()[i];
            textProduct.setText(Integer.toString(key));
            textAmount.setText(Integer.toString(sale.getProducts().get(key)));
            ProductController pc = new ProductController();
            textUnitPrice.setText(Double.toString(pc.findProduct(key).getSellingPrice()));
            textPrice.setText(Double.toString(pc.findProduct(key).getSellingPrice()*sale.getProducts().get(key)));
            sum+=pc.findProduct(key).getSellingPrice()*sale.getProducts().get(key);
            textTotalPrice.setText(Double.toString(sum));
        }

        labelNull = new JLabel("", JLabel.LEFT);
        labelNull.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 3;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelNull,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelDate,gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textDate,gbc);

        labelTotalPrice = new JLabel("Total Price:", JLabel.RIGHT);
        labelTotalPrice.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        panelSaleInvoice2.add(labelTotalPrice,gbc);

        gbc.gridx = 3;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textTotalPrice.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(textTotalPrice,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findSaleInvoice();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = y+4;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(80,25));
        panelSaleInvoice2.add(ok,gbc);
    }

    public void findSaleInvoiceSecondWindow(int saleId){
        generateSaleInvoiceSecondWindow(saleId);
        this.getContentPane().add(panelSaleInvoice2, BorderLayout.NORTH);
    }

    private void generateLeaseInvoice(){
        JTextField textLeaseId = new JTextField();
        JLabel labelLeaseId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textLeaseId.setText("");

        labelLeaseId = new JLabel("Lease ID:", JLabel.RIGHT);
        labelLeaseId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice.add(labelLeaseId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textLeaseId.setPreferredSize(new Dimension(100,25));
        panelLeaseInvoice.add(textLeaseId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findLeaseInvoiceSecondWindow(Integer.parseInt(textLeaseId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textLeaseId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findLeaseInvoice();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No note!");
                        textLeaseId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelLeaseInvoice.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    notesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textLeaseId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelLeaseInvoice.add(back,gbc);
    }

    public void findLeaseInvoice(){
        generateLeaseInvoice();
        this.getContentPane().add(panelLeaseInvoice, BorderLayout.NORTH);
    }

    public void generateLeaseInvoiceSecondWindow(int leaseId){
        Lease lease = LeaseContainer.getInstance().findLeaseById(leaseId);
        Date dNow = new Date( );
        SimpleDateFormat ft =  new SimpleDateFormat ("dd/MM/yyyy");

        JTextField textEmployeeId = new JTextField();
        JTextField textEmployeeName = new JTextField();
        JTextField textLeaseDate = new JTextField();
        JTextField textCustomerId = new JTextField();
        JTextField textCustomerName = new JTextField();
        JTextField textCustomerAddress = new JTextField();
        JLabel labelEmployeeId;
        JLabel labelEmployeeName;
        JLabel labelLeaseDate;
        JLabel labelCustomerId;
        JLabel labelCustomerName;
        JLabel labelCustomerAddress;

        JLabel labelProducts;
        JLabel labelProduct;
        JLabel labelAmount;
        JLabel labelUnitPrice;
        JLabel labelPrice;
        JTextField textProduct = new JTextField();
        JTextField textAmount = new JTextField();
        JTextField textUnitPrice = new JTextField();
        JTextField textPrice = new JTextField();

        JLabel labelNull;
        JLabel labelTotalPrice;
        JLabel labelDate;
        JTextField textTotalPrice = new JTextField();
        JTextField textDate = new JTextField();
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textEmployeeId.setEditable(false);
        textEmployeeName.setEditable(false);
        textLeaseDate.setEditable(false);
        textCustomerId.setEditable(false);
        textCustomerName.setEditable(false);
        textCustomerAddress.setEditable(false);
        textProduct.setEditable(false);
        textAmount.setEditable(false);
        textUnitPrice.setEditable(false);
        textPrice.setEditable(false);
        textTotalPrice.setEditable(false);
        textDate.setEditable(false);

        textEmployeeId.setText(Integer.toString(lease.getEmployeeID()));
        textEmployeeName.setText(EmployeeContainer.getInstance().findEmployeeById(lease.getEmployeeID()).getName());
        textLeaseDate.setText(lease.getCurrentDate());
        textCustomerId.setText(Integer.toString(lease.getCustomerID()));
        textCustomerName.setText(CustomerContainer.getInstance().findCustomerById(lease.getCustomerID()).getName());
        textCustomerAddress.setText(CustomerContainer.getInstance().findCustomerById(lease.getCustomerID()).getAddress());

        textDate.setText(ft.format(dNow));

        labelEmployeeId = new JLabel("Employee ID:", JLabel.RIGHT);
        labelEmployeeId.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelEmployeeId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeId.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textEmployeeId,gbc);

        labelEmployeeName = new JLabel("Employee Name:", JLabel.RIGHT);
        labelEmployeeName.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelEmployeeName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeName.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textEmployeeName,gbc);

        labelLeaseDate = new JLabel("Lease date:", JLabel.RIGHT);
        labelLeaseDate.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelLeaseDate, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textLeaseDate.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textLeaseDate,gbc);

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelCustomerId, gbc);

        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textCustomerId,gbc);

        labelCustomerName = new JLabel("Customer Name:", JLabel.RIGHT);
        labelCustomerName.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelCustomerName, gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerName.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textCustomerName,gbc);

        labelCustomerAddress = new JLabel("Customer Address:", JLabel.RIGHT);
        labelCustomerAddress.setPreferredSize(new Dimension(150,25));
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelCustomerAddress,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerAddress.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textCustomerAddress,gbc);

        labelProducts = new JLabel("Products:", JLabel.CENTER);
        labelProducts.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelProducts,gbc);

        labelProduct = new JLabel("Product:", JLabel.LEFT);
        labelProduct.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelProduct,gbc);

        labelAmount = new JLabel("Amount:", JLabel.LEFT);
        labelAmount.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelAmount,gbc);

        labelUnitPrice = new JLabel("Unit Price:", JLabel.LEFT);
        labelUnitPrice.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelUnitPrice,gbc);

        labelPrice = new JLabel("Price:", JLabel.LEFT);
        labelPrice.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 3;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelPrice,gbc);

        int y = 5;
        double sum =0;
        for(int i=0; i<lease.getProducts().size(); i++){

            textProduct = new JTextField();
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textProduct.setPreferredSize(new Dimension(80,25));
            panelLeaseInvoice2.add(textProduct,gbc);

            textAmount = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textAmount.setPreferredSize(new Dimension(80,25));
            panelLeaseInvoice2.add(textAmount,gbc);

            textUnitPrice = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textUnitPrice.setPreferredSize(new Dimension(80,25));
            panelLeaseInvoice2.add(textUnitPrice,gbc);

            textPrice = new JTextField();
            gbc.gridx = 3;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textPrice.setPreferredSize(new Dimension(80,25));
            panelLeaseInvoice2.add(textPrice,gbc);

            y++;
            textProduct.setEditable(false);
            textAmount.setEditable(false);
            textUnitPrice.setEditable(false);
            textPrice.setEditable(false);

            int key = (int) lease.getProducts().keySet().toArray()[i];
            textProduct.setText(Integer.toString(key));
            textAmount.setText(Integer.toString(lease.getProducts().get(key)));
            ProductController pc = new ProductController();
            textUnitPrice.setText(Double.toString(pc.findProduct(key).getLeasingPrice()));
            textPrice.setText(Double.toString(pc.findProduct(key).getLeasingPrice()*lease.getProducts().get(key)));
            sum+=pc.findProduct(key).getLeasingPrice()*lease.getProducts().get(key);
            textTotalPrice.setText(Double.toString(sum));
        }

        labelNull = new JLabel("", JLabel.LEFT);
        labelNull.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 3;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelNull,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelDate,gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textDate,gbc);

        labelTotalPrice = new JLabel("Total Price:", JLabel.RIGHT);
        labelTotalPrice.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        panelLeaseInvoice2.add(labelTotalPrice,gbc);

        gbc.gridx = 3;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textTotalPrice.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(textTotalPrice,gbc);

      
        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findLeaseInvoice();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 11;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(80,25));
        panelLeaseInvoice2.add(ok,gbc);
    }

    public void findLeaseInvoiceSecondWindow(int deliveryId){
        generateLeaseInvoiceSecondWindow(deliveryId);
        this.getContentPane().add(panelLeaseInvoice2, BorderLayout.NORTH);
    }

    private void generateDispatchNote(){
        JTextField textDispatchNoteId = new JTextField();
        JLabel labelDispatchNoteId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textDispatchNoteId.setText("");

        labelDispatchNoteId = new JLabel("Delivey ID:", JLabel.RIGHT);
        labelDispatchNoteId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote.add(labelDispatchNoteId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textDispatchNoteId.setPreferredSize(new Dimension(100,25));
        panelDispatchNote.add(textDispatchNoteId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findDispatchNoteSecondWindow(Integer.parseInt(textDispatchNoteId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textDispatchNoteId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findDispatchNote();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No note!");
                        textDispatchNoteId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDispatchNote.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    notesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textDispatchNoteId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDispatchNote.add(back,gbc);
    }

    public void findDispatchNote(){
        generateDispatchNote();
        this.getContentPane().add(panelDispatchNote, BorderLayout.NORTH);
    }

    public void generateDispatchNoteSecondWindow(int deliveryId){
        Delivery delivery = DeliveryContainer.getInstance().findDeliveryById(deliveryId);
        Date dNow = new Date( );
        SimpleDateFormat ft =  new SimpleDateFormat ("dd/MM/yyyy");

        JTextField textEmployeeId = new JTextField();
        JTextField textEmployeeName = new JTextField();
        JTextField textDispatchNoteDate = new JTextField();
        JTextField textCustomerId = new JTextField();
        JTextField textCustomerName = new JTextField();
        JTextField textCustomerAddress = new JTextField();
        JLabel labelEmployeeId;
        JLabel labelEmployeeName;
        JLabel labelDispatchNoteDate;
        JLabel labelCustomerId;
        JLabel labelCustomerName;
        JLabel labelCustomerAddress;

        JLabel labelProducts;
        JLabel labelProduct;
        JLabel labelAmount;
        JLabel labelUnitPrice;
        JLabel labelPrice;
        JTextField textProduct = new JTextField();
        JTextField textAmount = new JTextField();
        JTextField textUnitPrice = new JTextField();
        JTextField textPrice = new JTextField();

        JLabel labelNull;
        JLabel labelTotalPrice;
        JLabel labelDate;
        JTextField textTotalPrice = new JTextField();
        JTextField textDate = new JTextField();
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textEmployeeId.setEditable(false);
        textEmployeeName.setEditable(false);
        textDispatchNoteDate.setEditable(false);
        textCustomerId.setEditable(false);
        textCustomerName.setEditable(false);
        textCustomerAddress.setEditable(false);
        textProduct.setEditable(false);
        textAmount.setEditable(false);
        textUnitPrice.setEditable(false);
        textPrice.setEditable(false);
        textTotalPrice.setEditable(false);
        textDate.setEditable(false);

        textEmployeeId.setText(Integer.toString(delivery.getEmployeeID()));
        textEmployeeName.setText(EmployeeContainer.getInstance().findEmployeeById(delivery.getEmployeeID()).getName());
        textDispatchNoteDate.setText(delivery.getDate());
        textCustomerId.setText(Integer.toString(delivery.getCustomerID()));
        textCustomerName.setText(CustomerContainer.getInstance().findCustomerById(delivery.getCustomerID()).getName());
        textCustomerAddress.setText(CustomerContainer.getInstance().findCustomerById(delivery.getCustomerID()).getAddress());

        textDate.setText(ft.format(dNow));

        labelEmployeeId = new JLabel("Employee ID:", JLabel.RIGHT);
        labelEmployeeId.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelEmployeeId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeId.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(textEmployeeId,gbc);

        labelEmployeeName = new JLabel("Employee Name:", JLabel.RIGHT);
        labelEmployeeName.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelEmployeeName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeName.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(textEmployeeName,gbc);

        labelDispatchNoteDate = new JLabel("DispatchNote date:", JLabel.RIGHT);
        labelDispatchNoteDate.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelDispatchNoteDate, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textDispatchNoteDate.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(textDispatchNoteDate,gbc);

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelCustomerId, gbc);

        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(textCustomerId,gbc);

        labelCustomerName = new JLabel("Customer Name:", JLabel.RIGHT);
        labelCustomerName.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelCustomerName, gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerName.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(textCustomerName,gbc);

        labelCustomerAddress = new JLabel("Customer Address:", JLabel.RIGHT);
        labelCustomerAddress.setPreferredSize(new Dimension(150,25));
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelCustomerAddress,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerAddress.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(textCustomerAddress,gbc);

        labelProducts = new JLabel("Products:", JLabel.CENTER);
        labelProducts.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelProducts,gbc);

        labelProduct = new JLabel("Product:", JLabel.LEFT);
        labelProduct.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelProduct,gbc);

        labelAmount = new JLabel("Amount:", JLabel.LEFT);
        labelAmount.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelAmount,gbc);

        int y = 5;
        for(int i=0; i<delivery.getProducts().size(); i++){

            textProduct = new JTextField();
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textProduct.setPreferredSize(new Dimension(80,25));
            panelDispatchNote2.add(textProduct,gbc);

            textAmount = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textAmount.setPreferredSize(new Dimension(80,25));
            panelDispatchNote2.add(textAmount,gbc);

            y++;
            textProduct.setEditable(false);
            textAmount.setEditable(false);

            int key = (int) delivery.getProducts().keySet().toArray()[i];
            textProduct.setText(Integer.toString(key));
            textAmount.setText(Integer.toString(delivery.getProducts().get(key)));

        }

        labelNull = new JLabel("", JLabel.LEFT);
        labelNull.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 3;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelNull,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDispatchNote2.add(labelDate,gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(textDate,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findDispatchNote();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = y+4;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(80,25));
        panelDispatchNote2.add(ok,gbc);
    }

    public void findDispatchNoteSecondWindow(int deliveryId){
        generateDispatchNoteSecondWindow(deliveryId);
        this.getContentPane().add(panelDispatchNote2, BorderLayout.NORTH);
    }
    
    private void generateDeliveryNote(){
        JTextField textDeliveryNoteId = new JTextField();
        JLabel labelDeliveryNoteId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textDeliveryNoteId.setText("");

        labelDeliveryNoteId = new JLabel("Delivey ID:", JLabel.RIGHT);
        labelDeliveryNoteId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote.add(labelDeliveryNoteId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textDeliveryNoteId.setPreferredSize(new Dimension(100,25));
        panelDeliveryNote.add(textDeliveryNoteId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findDeliveryNoteSecondWindow(Integer.parseInt(textDeliveryNoteId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textDeliveryNoteId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findDeliveryNote();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No note!");
                        textDeliveryNoteId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDeliveryNote.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    notesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textDeliveryNoteId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDeliveryNote.add(back,gbc);
    }

    public void findDeliveryNote(){
        generateDeliveryNote();
        this.getContentPane().add(panelDeliveryNote, BorderLayout.NORTH);
    }

    public void generateDeliveryNoteSecondWindow(int deliveryId){
        Delivery delivery = DeliveryContainer.getInstance().findDeliveryById(deliveryId);
        Date dNow = new Date( );
        SimpleDateFormat ft =  new SimpleDateFormat ("dd/MM/yyyy");

        JTextField textEmployeeId = new JTextField();
        JTextField textEmployeeName = new JTextField();
        JTextField textDeliveryNoteDate = new JTextField();
        JTextField textCustomerId = new JTextField();
        JTextField textCustomerName = new JTextField();
        JTextField textCustomerAddress = new JTextField();
        JLabel labelEmployeeId;
        JLabel labelEmployeeName;
        JLabel labelDeliveryNoteDate;
        JLabel labelCustomerId;
        JLabel labelCustomerName;
        JLabel labelCustomerAddress;

        JLabel labelProducts;
        JLabel labelProduct;
        JLabel labelAmount;
        JLabel labelUnitPrice;
        JLabel labelPrice;
        JTextField textProduct = new JTextField();
        JTextField textAmount = new JTextField();
        JTextField textUnitPrice = new JTextField();
        JTextField textPrice = new JTextField();

        JLabel labelNull;
        JLabel labelTotalPrice;
        JLabel labelDate;
        JTextField textTotalPrice = new JTextField();
        JTextField textDate = new JTextField();
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textEmployeeId.setEditable(false);
        textEmployeeName.setEditable(false);
        textDeliveryNoteDate.setEditable(false);
        textCustomerId.setEditable(false);
        textCustomerName.setEditable(false);
        textCustomerAddress.setEditable(false);

        textTotalPrice.setEditable(false);
        textDate.setEditable(false);

        textEmployeeId.setText(Integer.toString(delivery.getEmployeeID()));
        textEmployeeName.setText(EmployeeContainer.getInstance().findEmployeeById(delivery.getEmployeeID()).getName());
        textDeliveryNoteDate.setText(delivery.getDate());
        textCustomerId.setText(Integer.toString(delivery.getCustomerID()));
        textCustomerName.setText(CustomerContainer.getInstance().findCustomerById(delivery.getCustomerID()).getName());
        textCustomerAddress.setText(CustomerContainer.getInstance().findCustomerById(delivery.getCustomerID()).getAddress());
        textTotalPrice.setText(Double.toString(delivery.getPrice()));
        textDate.setText(ft.format(dNow));

        labelEmployeeId = new JLabel("Employee ID:", JLabel.RIGHT);
        labelEmployeeId.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelEmployeeId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeId.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textEmployeeId,gbc);

        labelEmployeeName = new JLabel("Employee Name:", JLabel.RIGHT);
        labelEmployeeName.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelEmployeeName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textEmployeeName.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textEmployeeName,gbc);

        labelDeliveryNoteDate = new JLabel("DeliveryNote date:", JLabel.RIGHT);
        labelDeliveryNoteDate.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelDeliveryNoteDate, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textDeliveryNoteDate.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textDeliveryNoteDate,gbc);

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelCustomerId, gbc);

        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textCustomerId,gbc);

        labelCustomerName = new JLabel("Customer Name:", JLabel.RIGHT);
        labelCustomerName.setPreferredSize(new Dimension(150, 25));
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelCustomerName, gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerName.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textCustomerName,gbc);

        labelCustomerAddress = new JLabel("Customer Address:", JLabel.RIGHT);
        labelCustomerAddress.setPreferredSize(new Dimension(150,25));
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelCustomerAddress,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerAddress.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textCustomerAddress,gbc);

        labelProducts = new JLabel("Products:", JLabel.CENTER);
        labelProducts.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelProducts,gbc);

        labelProduct = new JLabel("Product:", JLabel.LEFT);
        labelProduct.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelProduct,gbc);

        labelAmount = new JLabel("Amount:", JLabel.LEFT);
        labelAmount.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelAmount,gbc);

        int y = 5;
        for(int i=0; i<delivery.getProducts().size(); i++){

            textProduct = new JTextField();
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textProduct.setPreferredSize(new Dimension(80,25));
            panelDeliveryNote2.add(textProduct,gbc);

            textAmount = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textAmount.setPreferredSize(new Dimension(80,25));
            panelDeliveryNote2.add(textAmount,gbc);

            y++;
            textProduct.setEditable(false);
            textAmount.setEditable(false);

            int key = (int) delivery.getProducts().keySet().toArray()[i];
            textProduct.setText(Integer.toString(key));
            textAmount.setText(Integer.toString(delivery.getProducts().get(key)));

        }

        labelNull = new JLabel("", JLabel.LEFT);
        labelNull.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 3;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelNull,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(80,25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelDate,gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textDate,gbc);

        labelTotalPrice = new JLabel("Delivery costs:", JLabel.RIGHT);
        labelTotalPrice.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveryNote2.add(labelTotalPrice,gbc);

        gbc.gridx = 3;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textTotalPrice.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(textTotalPrice,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findDeliveryNote();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = y+4;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(80,25));
        panelDeliveryNote2.add(ok,gbc);
    }

    public void findDeliveryNoteSecondWindow(int deliveryId){
        generateDeliveryNoteSecondWindow(deliveryId);
        this.getContentPane().add(panelDeliveryNote2, BorderLayout.NORTH);
    }
}